package com.ljh.mboxvidoserver.main.controller;


import com.ljh.mboxvidoserver.system.entity.LoginUser;
import com.ljh.mboxvidoserver.main.entity.Menu;
import com.ljh.mboxvidoserver.main.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "main")
public class MainController {

    @Autowired
    private MenuService menuService;

    @RequestMapping(value = "mainPage",method = RequestMethod.GET)
    public String loginOk(HttpSession session){
        LoginUser user = (LoginUser)session.getAttribute("loginUser");

        if(user==null){
            return "redirect:/login";
        }
        else{
            return "main/console";
        }
    }

    @ResponseBody
    @RequestMapping(value = "loadSysMenu")
    public List<Menu> loadSysMenu(Menu menu){
        List<Menu> menuList = null;
        try {
            Integer pid = menu.getPid();
            if(pid!=null) {
                menuList = menuService.findMenuListByPid(pid);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return menuList;
    }

    @RequestMapping(value = "topPage")
    public String mainPage(){
        return "main/top";
    }
}
